#include <iostream>
#include "../types.h"
#include "module.h"

namespace todo { namespace mod {
	
void TodoModule::callMethod(const std::string& class_name, const std::string& method_name)
{
	std::map<std::string, TodoClass*>::iterator it;
	std::map<std::string, TodoMethod*>::iterator it2;
	
	it = classes.find(class_name);
	
	if (it == classes.end()) {
		throw todo_exception("Class `" + class_name + "' not found!");
	}
	
	it2 = it->second->methods.find(method_name);
		
	if (it2 == it->second->methods.end()) {
		throw todo_exception("Method `" + method_name + "' not found!");
	}
	
	// Calling the method
	(this->*(it2->second->handler))();
}

void TodoModule::addClassModule(const char* name, TodoClass* cls)
{
	classes.insert(std::make_pair<std::string, TodoClass*>(std::string(name), cls));
}

void TodoModules::add(TodoModule* module)
{
	module->init();
	modules.insert(std::make_pair<std::string, TodoModule*>(module->getName(), module));
}

TodoModule* TodoModules::getModule(const std::string& name)
{
	std::map<std::string, TodoModule*>::iterator it;
	
	it = modules.find(name);
	
	return it->second;	
}

}} // todo::mod
